package com.ray.controller;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import javax.management.RuntimeErrorException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.exception.ServiceException;
import com.ray.service.PutOutServiceNew;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;

//外发
@Controller
@RequestMapping("/putOutNew")
public class PutOutControllerNew {

	@Autowired
	PutOutServiceNew putOutServiceNew;

	@RequestMapping("/{methodName}")
	@ResponseBody
	private Map<String, Object> addAirFlowControl(HttpServletRequest request,HttpServletResponse response,
			@PathVariable("methodName") String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException {
		Map<String, Object> requestParamMap = RequestUtil.getRequestAllParam(request);
		requestParamMap.put("request", request);
		requestParamMap.put("response", response);
		Method targetMethod = putOutServiceNew.getClass().getMethod(methodName, Map.class);
		Object object=null;
		try {
			 object=targetMethod.invoke(putOutServiceNew, requestParamMap);
		}catch(InvocationTargetException e) {
			if(e.getCause().getClass()== IllegalArgumentException.class || e.getCause().getClass() ==ServiceException.class) {
				e.printStackTrace();
				Assert.notNull(null,e.getTargetException().getMessage());
			}else {
				e.printStackTrace();
				return ResponseUtil.ResponseError("网络不好，请稍等。。。。。");
			}
		}
		return ResponseUtil.ResponseResult(object);
	}

}
